package com.shinemo.mpush.alloc.utils;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import com.google.common.collect.Lists;
import com.mpush.tools.common.HashUtil;
import com.mpush.tools.config.CC;

public class HttpHeaderUtils {
	
	public static final String CALL_TIMESTAMP = "callTimestamp";
	public static final String SIGN = "sign";
	
	private HttpHeaderUtils() {
		
	}
	
	
	public static Map<String,String> get(Map<String,String> params){
		
		Map<String, String> headers=new HashMap<String,String>(0);
		headers.put(CALL_TIMESTAMP, System.currentTimeMillis()+"");
		headers.put(SIGN, getSign(params));
		
		return headers;
	}

	public static String getSign(Map<String, String> params) {
		 String queryString = Lists.newArrayList(params.keySet()).stream().filter(Objects::nonNull).sorted()
				.map(key -> String.join("=", key, params.get(key))).collect(Collectors.joining("&"));
		 return HashUtil.sha1(queryString + CC.mp.api.signSecret);
	}
	

}
